package com.atwisdom.star.core.bsl.planner.entity;

import cn.hutool.core.collection.CollectionUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class ProductBomMaster {
    private Long id;
    private String nodeId;
    private String parentId;
    private String bomCode;
    private Long materialId;
    private String materialPCode;  //品番
    private String materialBCode;  //背番
    private String materialName;  //背番
    private String materialUnit;   //主单位
    private Integer invertWarehouse;
    private Integer invertLocation;
    private boolean enable;
    private double bomVersion;
    private Double quantity;
    private Double lossRate = 0.0;
    private String parentMaterialBCode;
    private String bomType;
    private List<ProductBomSub> productBomSubList = new ArrayList<>();  //副产品
    private List<ProductBomSub> materialBomSubList = new ArrayList<>(); //原料

    public boolean existSubMaterialBCode(String bCode){
        if(CollectionUtil.isNotEmpty(productBomSubList)){
            ProductBomSub productBomSub = productBomSubList.stream().filter(d -> d.getMaterialBCode().equalsIgnoreCase(bCode)).findFirst().orElse(null);
            if(productBomSub!=null){
               return true;
            }
        }
        return false;
    }

    public String getSplitBomCode(String bCode){
        if(CollectionUtil.isNotEmpty(productBomSubList)){
            ProductBomSub productBomSub = productBomSubList.stream().filter(d -> d.getMaterialBCode().equalsIgnoreCase(bCode)).findFirst().orElse(null);
            if(productBomSub!=null && !productBomSub.getSplitBomCode().equalsIgnoreCase("NaN")){
                return productBomSub.getSplitBomCode();
            }
        }
        return null;
    }

    public ProductBomSub getProductBomSub(String bCode){
        if(CollectionUtil.isNotEmpty(productBomSubList)){
            ProductBomSub productBomSub = productBomSubList.stream().filter(d -> d.getMaterialBCode().equalsIgnoreCase(bCode)).findFirst().orElse(null);
            if(productBomSub!=null){
                return productBomSub;
            }
        }
        return null;
    }

    /**
     * 获取副产品列表
     * @return 返回副产品列表
     */
    public List<ProductBomSub> getOtherProductBomSub(String bCode){
       return productBomSubList.stream().filter(d -> !d.getMaterialBCode().equalsIgnoreCase(bCode)).collect(Collectors.toList());
    }
}
